//
//  ADStaticTableViewModel.m
//  ADKit
//
//  Created by duzexu on 2018/3/29.
//

#import "ADStaticTableViewModel.h"

@implementation ADStaticTableGroupModel

- (instancetype)init {
    self = [super init];
    if (self) {
        _footerHeight = 20;
        _headerHeight = CGFLOAT_MIN;
    }
    return self;
}

@end

@implementation ADStaticTableItemModel

+ (instancetype)itemWithTitle:(NSString *)title detail:(NSString *)detail icon:(NSString *)icon {
    ADStaticTableItemModel *item = [[self alloc] init];
    item.title = title;
    item.detail = detail;
    item.icon = icon;
    return item;
}

+ (instancetype)itemWithTitle:(NSString *)title icon:(NSString *)icon {
    return [self itemWithTitle:title detail:nil icon:icon];
}

+ (instancetype)itemWithTitle:(NSString *)title detail:(NSString *)detail {
    return [self itemWithTitle:title detail:detail icon:nil];
}

+ (instancetype)itemWithTitle:(NSString *)title {
    return [self itemWithTitle:title icon:nil];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        _selectionStyle = UITableViewCellSelectionStyleNone;
        _accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        _rowHeight = 44.0f;
    }
    return self;
}

- (NSString *)cellReuseIdentify {
    return @"UITableViewCell";
}

@end

@implementation ADStaticTableViewModel

- (void)initialize {
    [super initialize];
    self.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.viewControllerClass = [ADStaticTableViewController class];
}

- (void)setStaticTableModels:(NSArray *)models {
    if ([models isKindOfClass:NSArray.class]) {
        [self.dataSource removeAllObjects];
        [self.dataSource addObjectsFromArray:models];
        [self reloadDataSource];
    }
}

- (ADStaticTableItemModel *)itemWithIndexPath:(NSIndexPath*)indexPath {
    if (indexPath.section < self.dataSource.count) {
        ADStaticTableGroupModel *group = self.dataSource[indexPath.section];
        if (indexPath.row < group.itemModels.count) {
            ADStaticTableItemModel *item = group.itemModels[indexPath.row];
            return item;
        }
    }
    return nil;
}

- (void)reloadWithIndexPath:(NSIndexPath *)indexPath block:(AD_VoidBlock_id)block {
    if (block) {
        ADStaticTableItemModel *item = [self itemWithIndexPath:indexPath];
        block(item);
        block = nil;
    }
    [self reloadDataSourceWithIndexPath:indexPath];
}

#pragma mark - sub class can override it
- (NSString *)tableView:(UITableView *)tableView cellIdentifierForIndexPath:(NSIndexPath *)indexPath withObject:(ADStaticTableItemModel *)object {
    return object.cellReuseIdentify;
}

- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(ADStaticTableItemModel *)object {
    cell.textLabel.text = object.title;
    cell.detailTextLabel.text = object.detail;
    cell.imageView.image = [UIImage imageNamed:object.icon];
    cell.selectionStyle = object.selectionStyle;
    cell.accessoryType = object.accessoryType;
    if (object.textColor) {
        cell.textLabel.textColor = object.textColor;
    }
    if (object.detailTextColor) {
        cell.detailTextLabel.textColor = object.detailTextColor;
    }
    if (object.titleFont) {
        cell.textLabel.font = object.titleFont;
    }
    if (object.detailFont) {
        cell.detailTextLabel.font = object.detailFont;
    }
    if (object.indicatorImage) {
        cell.accessoryView = [[UIImageView alloc] initWithImage:object.indicatorImage];
    }
    if (object.backgroundColor) {
        cell.backgroundColor = object.backgroundColor;
    }
    if ([cell conformsToProtocol:@protocol(ADReactiveView)]) {
        [cell performSelector:@selector(bindViewModel:) withObject:object];
    }
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    ADStaticTableGroupModel *group = self.dataSource[section];
    return group.itemModels.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // fetch object
    ADStaticTableGroupModel *group = self.dataSource[indexPath.section];
    ADStaticTableItemModel *model = group.itemModels[indexPath.row];
    
    NSString *cellIdentifier = [self tableView:tableView cellIdentifierForIndexPath:indexPath withObject:model];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    /// bind model
    [self configureCell:cell atIndexPath:indexPath withObject:model];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ADStaticTableGroupModel *group = self.dataSource[indexPath.section];
    ADStaticTableItemModel *model = group.itemModels[indexPath.row];
    return model.rowHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    ADStaticTableGroupModel *group = self.dataSource[section];
    return group.headerHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    ADStaticTableGroupModel *group = self.dataSource[section];
    return group.footerHeight;
}

@end
